/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelHorarioCargo.java
 *
 * Created on Apr 16, 2011, 2:39:40 PM
 */

package frontera.interfaz.panel;

import control.SIRHApp;
import entidad.Horario;
import entidad.HorarioDia;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Nico
 */
public class PanelHorarioCargo extends javax.swing.JPanel {

    /** Creates new form PanelHorarioCargo */
    public PanelHorarioCargo(String nombre,Horario horario, JPanel panel) {
        initComponents();
        this.panel = panel;
        inicializarVariables(nombre, horario);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        labelNombreCargo = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        spinnerInicio = new javax.swing.JSpinner();
        spinnerFin = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        boxDiasSemama = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        botonAtras = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        panelHorarioCargo = new javax.swing.JPanel();

        jLabel5.setFont(new java.awt.Font("Dialog", 1, 30));
        jLabel5.setText("Editando Horario");

        labelNombreCargo.setFont(new java.awt.Font("Tahoma", 0, 18));
        labelNombreCargo.setText("[Nombre del Cargo]");

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/Schedule.png"))); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Definicion de horario"));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        spinnerInicio.setEnabled(false);
        spinnerInicio.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerInicioStateChanged(evt);
            }
        });
        jPanel1.add(spinnerInicio, new org.netbeans.lib.awtextra.AbsoluteConstraints(122, 69, 39, -1));

        spinnerFin.setEnabled(false);
        spinnerFin.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerFinStateChanged(evt);
            }
        });
        jPanel1.add(spinnerFin, new org.netbeans.lib.awtextra.AbsoluteConstraints(122, 107, 39, -1));

        jLabel1.setText("Hora de Entrada");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 72, -1, -1));

        jLabel2.setText("Hora de Salida");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 110, -1, -1));

        jLabel3.setText("Dia de la Semana");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(16, 34, -1, -1));

        boxDiasSemama.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo" }));
        boxDiasSemama.setSelectedItem(null
        );
        boxDiasSemama.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxDiasSemamaActionPerformed(evt);
            }
        });
        jPanel1.add(boxDiasSemama, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 30, -1, -1));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Opciones de Manejo"));

        jLabel7.setText("1. Indique el dia a modificar, la hora ");

        jLabel8.setText("de entrada y la hora de salida.");

        jLabel9.setText("de entrada hasta la hora de salida.");

        jLabel10.setText("2. Seleccione las horas desde la hora");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel8))
                    .addComponent(jLabel7)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel9))
                    .addComponent(jLabel10))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addContainerGap(19, Short.MAX_VALUE))
        );

        botonAtras.setText("Atras");
        botonAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAtrasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(botonAtras, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonAtras)
                .addContainerGap())
        );

        panelHorarioCargo.setLayout(new java.awt.GridLayout(1, 0));
        jScrollPane1.setViewportView(panelHorarioCargo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNombreCargo)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelNombreCargo)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void inicializarVariables(String nombre,Horario horario){
        this.horario = horario;
        
        horarioCargo = new PanelHorario(horario);
        panelHorarioCargo.add(horarioCargo);
        
        SpinnerNumberModel modelIni = new SpinnerNumberModel(0, 0, 24, 1);
        spinnerInicio.setModel(modelIni);
        
        this.labelNombreCargo.setText(nombre);
        
    }
    private void boxDiasSemamaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxDiasSemamaActionPerformed
        spinnerInicio.setEnabled(true);
        Horario temp = horarioCargo.getHorarioSeleccionado();
        
        int index = boxDiasSemama.getSelectedIndex();
        List<HorarioDia> horarioTMP = temp.getHorario();
        int iniJor = horarioTMP.get(index).getInicioJor();
        
        SpinnerNumberModel model = new SpinnerNumberModel(iniJor,iniJor,24,1);
        
        this.spinnerFin.setModel(model);
        this.spinnerInicio.setValue((Integer)temp.getHorario().get(index).getInicioJor());
        this.spinnerFin.setValue((Integer)temp.getHorario().get(index).getFinJor());
    }//GEN-LAST:event_boxDiasSemamaActionPerformed

    private void spinnerInicioStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerInicioStateChanged
       this.spinnerFin.setEnabled(true);
       this.spinIniValue = (Integer)spinnerInicio.getValue();
       this.spinFinValue = (Integer)spinnerFin.getValue();
       
       SpinnerNumberModel modelFin = new SpinnerNumberModel(spinIniValue,spinIniValue, 24, 1);
       this.spinnerFin.setModel(modelFin);
       
       Horario temp = horarioCargo.getHorarioSeleccionado();
       
       int index = boxDiasSemama.getSelectedIndex();
       int iniJor = temp.getHorario().get(index).getInicioJor();
       
       if(iniJor >= 0 && spinFinValue > spinIniValue)
       {
           spinnerFin.setValue(spinFinValue);
       }
       seleccionGrafica();
 
    }//GEN-LAST:event_spinnerInicioStateChanged

    private void spinnerFinStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerFinStateChanged
       this.spinFinValue = (Integer)spinnerFin.getModel().getValue();
       this.spinIniValue = (Integer)spinnerInicio.getModel().getValue();
       seleccionGrafica();
    }//GEN-LAST:event_spinnerFinStateChanged

    private void botonAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAtrasActionPerformed
        horario.setHorario(horarioCargo.getHorarioSeleccionado().getHorario());
        SIRHApp.getInstance().getVentanaPrincipal().setPanelPersonalCargo(panel);
        
    }//GEN-LAST:event_botonAtrasActionPerformed
    private void seleccionGrafica(){
       int index = boxDiasSemama.getSelectedIndex();
       this.horarioCargo.seleccionarHoras(index+1,this.spinIniValue,this.spinFinValue);
       horario.setHorarioDia(index, spinIniValue, spinFinValue);
       horario.getHorario().get(index).setHorario(horario);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAtras;
    private javax.swing.JComboBox boxDiasSemama;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelNombreCargo;
    private javax.swing.JPanel panelHorarioCargo;
    private javax.swing.JSpinner spinnerFin;
    private javax.swing.JSpinner spinnerInicio;
    // End of variables declaration//GEN-END:variables
    private JPanel panel;
    private PanelHorario horarioCargo;
    private Horario horario;
    private int spinIniValue;
    private int spinFinValue;

 
}
